package com.ocm.tourism.core.dao.jpa;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.ocm.tourism.core.dao.CommonItemsDao;
import com.ocm.tourism.core.domain.CommonItems;
import com.ocm.tourism.core.domain.ItemTypes;

@Repository("commonDao")
public class JpaCommonItemsDao extends JpaGenericDao<CommonItems> implements CommonItemsDao {

   public JpaCommonItemsDao() {
      this.setClazz(CommonItems.class);
   }

   @Override
   public List<CommonItems> findAll() {
      return entityManager().createQuery("SELECT o FROM CommonItems o", CommonItems.class).getResultList();
   }

   public long count() {
      return entityManager().createQuery("SELECT COUNT(o) FROM CommonItems o", Long.class).getSingleResult();
   }

   @Override
   public List<CommonItems> findAll(int firstResult, int maxResults) {
      return entityManager().createQuery("SELECT o FROM CommonItems o", CommonItems.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
   }

   @SuppressWarnings("unchecked")
   public List<CommonItems> findByType(ItemTypes itemTypes) {
      Query query = entityManager().createQuery("SELECT o FROM CommonItems o WHERE o.itemTypes =:itemtype", CommonItems.class);
      query.setParameter("itemtype", itemTypes);
      return query.getResultList();
   }

}
